import 'package:kong/domain/model/user.dart';
import 'package:kong/domain/repository/local_storage_repository.dart';
import 'package:shared_preferences/shared_preferences.dart';

const _pref_token = 'TOKEN';
const _pref_username = 'USERNAME';
const _pref_nickname = 'NICKNAME';
const _pref_avatar = 'AVATAR';

class LocalRepositoryImpl extends LocalRepositoryInterface {
  @override
  Future<void> clearAllData() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.clear();
  }

  @override
  Future<String> getToken() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.getString(_pref_token);
  }

  @override
  Future<bool> setToken(String token) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    return sharedPreferences.setString(_pref_token, token);
  }

  @override
  Future<User> getUser() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    final _username = sharedPreferences.getString(_pref_username);
    final _nickname = sharedPreferences.getString(_pref_nickname);
    final _avatar = sharedPreferences.getString(_pref_avatar);
    return User(
      nickname: _nickname,
      username: _username,
      avatar: _avatar,
    );
  }

  @override
  Future<List<bool>> setUser(User user) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    final _usernameResult =
        await sharedPreferences.setString(_pref_username, user.username);
    final _nicknameResult =
        await sharedPreferences.setString(_pref_nickname, user.nickname);
    final _avatarResult =
        await sharedPreferences.setString(_pref_avatar, user.avatar);
    return [_usernameResult, _nicknameResult, _avatarResult];
  }
}
